<?php
/**
 * 《考勤明细》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/KqUtil.class.php');

class Kq extends KqUtil {
	public $tbl = Constant::tbl_emp;

	public $office = array(
		'-' => '请选择',
		'1' => '南山',
		'2' => '沙井',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '在职',
		'2' => '休假',
		'3' => '离职',
		'8' => '帐号',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'code' => '编号',
		'name' => '姓名',
		'dept' => '部门',
		'`join`' => '入职时间',
		'orderby' => '排序',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('office_option', $this->office);
	}

	function mobile($controller, $request) {
		$this->pretreat($controller);
	
		global $CategoryTree;
	
		$data = $CategoryTree['考勤管理'];
	
		$ret = array();
		foreach ($data as $k => $v) {
			if ($this->tree_my($v)) {
				$ret[$k] = $v;
			}
		}
	
		$controller->assign('menu', $ret);
	
		$controller->display($request, 'mobile');
		return true;
	}
	
	function tab($controller, $request) {		
		$data = array(			
			'每日生产' => array('cc'=>'mold.php?do=Work.find', 'js'=>'1'),
			'正班信息' => array('cc'=>'kq.php?do=Plan.find', 'js'=>'1'),
			'加班信息' => array('cc'=>'kq.php?do=Overtime.find', 'js'=>'1'),
			
			'请假信息' => array('cc'=>'kq.php?do=Vacation.find', 'js'=>'1'),
			'打卡明细' => array('cc'=>'kq.php?do=Record.find', 'js'=>'1'),
		
			'统计分析' => array('cc'=>'kq.php?do=Kq.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'货运需求' => array('cc'=>'express.php?do=Requirement.group', 'js'=>''),
			'货运安排' => array('cc'=>'express.php?do=Plan.group', 'js'=>''),		
			'进口业务' => array('cc'=>'express.php?do=In.group', 'js'=>''),				
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'kq/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {

		$code_s = $request['code_s'];
		$name_s = $request['name_s'];

		$t1 = $request['date1_s'];
		$t2 = $request['date2_s'];

		$where = " WHERE 1=1";

		if (!empty($code_s)) {
			$where .= " AND code like '" . $code_s . "%'";
		}

		if (!empty($name_s)) {
			$where .= " AND name like '" . $name_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['office_t'] = $this->office[$row['office']];
				$row['dept_t'] = $this->_dept($row['dept']);

				$row['plan'] = $this->_plan($row['id'], $t1, $t2);
				$row['overtime'] = $this->_overtime($row['id'], $t1, $t2);
				$row['vacation'] = $this->_vacation($row['id'], $t1, $t2);

				$row['status_t'] = $this->status[$row['status']];

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'kq/list');
		return true;
	}

	function _plan($emp, $t1, $t2) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE emp = '%s' AND t1 >= '%s' AND t2 < '%s'";
		$sql = sprintf($fmt, Constant::tbl_kq_plan, $emp, $t1, $t2);
		debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}

	function _overtime($emp, $t1, $t2) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE emp = '%s' AND t1 >= '%s' AND t2 < '%s'";
		$sql = sprintf($fmt, Constant::tbl_kq_overtime, $emp, $t1, $t2);
		debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}

	function _vacation($emp, $t1, $t2) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE emp = '%s' AND t1 >= '%s' AND t2 < '%s'";
		$sql = sprintf($fmt, Constant::tbl_kq_vacation, $emp, $t1, $t2);
		debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function kqcard($controller, $request) {
		$db = newdb($this->dsn_w);

		require_once APP_LIB . 'excel_class.php';
		$file = "d:/kqcard.xls";

		Read_Excel_File($file, $return);

		// debug(print_r($return, true));

		$buf = $return['Sheet1'];

		//统计cexel表中记录条数，循环
		for($j = 1; $j < count($buf); $j++) {
			$a = $buf[$j];

			$content = print_r($a, true);

			// debug($content);

			$kqcard = $a[4];
			$name = $a[1];

			if (!preg_match("/^(\d+)$/", $kqcard)) {
				continue;
			}

			$fmt = "UPDATE %s SET kqcard = '%s' WHERE name = '%s'";
			$sql = sprintf($fmt, Constant::tbl_emp, $kqcard, $name);
			debug($sql);
			$db->query($sql);
		}

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function b($controller, $request) {
		
	
		logdate($controller, $request);
		return true;
	}

	function t($controller, $request) {
		header("Content-type: text/html; charset=utf-8");

		// APP_LIB . 'MySQL.class.php'
				
		// include "TopSdk.php";
		require_once(APP_LIB . '/taobao-sdk-PHP-online_standard-20150111/TopSdk.php');
		
		//将下载SDK解压后top里的TopClient.php第8行$gatewayUrl的值改为沙箱地址:http://gw.api.tbsandbox.com/router/rest,
		
		//正式环境时需要将该地址设置为:http://gw.api.taobao.com/router/rest
		
		 
		
		//实例化TopClient类
		
		$c = new TopClient;
		
		$c->appkey = "23075164";
		
		$c->secretKey = "ceb324aa093754f12d7d186cd205148d";
		
		$sessionkey= "test";   //如沙箱测试帐号sandbox_c_1授权后得到的sessionkey
		
		//实例化具体API对应的Request类
		
		$req = new UserSellerGetRequest;
		
		$req->setFields("nick,user_id,type");
		
		//$req->setNick("sandbox_c_1");
		
		 
		
		//执行API请求并打印结果
		
		$resp = $c->execute($req,$sessionkey);
		
		echo "result:";
		
		print_r($resp);

		logdate($controller, $request);
		return true;
	}
}
?>